<?php
	include("includes/secure.php");
	include("includes/config.php");
	include_once("includes/class/case.class.php");
	include_once("includes/class/request.class.php");	
	
	$myRequest = new MyRequest(); 
	$myCase = new MyCase(); 
	
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	$use_code = 0;
	$use_name = '';
	$cli_code = 0;
	$cli_name = '';
	$cli_lastname = '';
	$case_number = 0;
	$ban_code = 0;
	$baco_code = 0;
	$bankContactList = array ();
	$countBankContactList = 0;
	
	
	$bankList = $myRequest->bankList();	
	
	$cli_code = 0;
	if ( isset( $_GET["case_code"] ) && (int) $_GET["case_code"] > 0 ){
		$case_code = (int) $_GET["case_code"];
		$dataCase = $myCase->getCase($case_code);
		
		$use_code = $dataCase->use_code;
		$use_name = $dataCase->use_name;
		$cli_code = $dataCase->cli_code;
		$cli_name = $dataCase->cli_name;
		$cli_lastname = $dataCase->cli_lastname;
		$case_number = $dataCase->case_number;
	}
	elseif ( isset( $_GET["req_code"] ) && (int) $_GET["req_code"] > 0 ){
		$req_code = $_GET["req_code"];
		$dataRequest = $myRequest->getRequestData($req_code);
		
		$case_code = $dataRequest->case_code;
		$use_code = $dataRequest->use_code;
		$use_name = $dataRequest->use_name;
		$cli_code = $dataRequest->cli_code;
		$cli_name = $dataRequest->cli_name;
		$cli_lastname = $dataRequest->cli_lastname;
		$case_number = $dataRequest->case_number;
		$ban_code = $dataRequest->ban_code;
		$baco_code = $dataRequest->baco_code;
		$loan_number = $dataRequest->loan_number;
		
		$bankContactList = $myRequest->bankContactList($ban_code);
		$countBankContactList = count ($bankContactList);
	}
	else
		$listClients = $myCase->listClients();
	
	if(isset($_POST['add']) && $_POST['add'] == 1){
		$data->req_code = $_POST['req_code'];
		$data->case_code = trim(mysql_real_escape_string($_POST['case_code']));
		$data->ban_code = trim(mysql_real_escape_string($_POST['ban_code']));
		$data->baco_code = trim(mysql_real_escape_string($_POST['baco_code']));
		$data->loan_number = trim(mysql_real_escape_string($_POST['loan_number']));
		
		if ( $data->req_code ){
			if ( $myRequest->updateRequest($data) ){
				$msg_info = true;
				$_SESSION["msg_cool"] .= 'The Business with the bank was updated successfully.<br />';
				
				echo "<script> window.location.href='listRequest.php?show_msg=3'</script>";
			}else{
				$msg_info = true;
				$_SESSION["msg_cool"] .= 'It could not be updated the request.<br />';
				echo "<script> window.location.href='listRequest.php?show_msg=1'</script>";
			}			
		}elseif ( $data->req_code = $myRequest->addRequest($data) ){
			$msg_info = true;
			$_SESSION["msg_cool"] .= 'The Business with the bank was added successfully.<br />';			
			
			echo "<script> window.location.href='listRequest.php?show_msg=3'</script>";
		}
		else{
			$msg_info = true;
			$_SESSION["msg_cool"] .= 'It could not be added the request.<br />';
			echo "<script> window.location.href='listBusiness.php?show_msg=1'</script>";
		}
	}
	
	if ( $msg_error )
		$show_msg = 1;
	elseif ( $msg_alert )
		$show_msg = 2;
	elseif ( $msg_info )
		$show_msg = 3;
?>